/*
 *校验规则： 在标签名添加属性
 * 1、不为空 ：    required
 * 2、最小长度、最大长度：minlength="5"  maxlength="5"
 * 3、使用自定义规则：      data-rule-自定义方法名="true"
 * 		
 */
//0-1小数
jQuery.validator.addMethod("isDecimal", function(value, element) {
	 var chrnum = /^(0\.(?!0+$)\d{1,3}|1(\.0{1,3})?)$/;  
	    return this.optional(element) || (chrnum.test(value)); 
	
}, "请输写正确折扣");

//不为0
jQuery.validator.addMethod("notZero", function(value, element) {
	var aint = parseInt(value);
	 return aint != 0 && (aint + "") == value; 
	
}, "必选");

//为1/2
jQuery.validator.addMethod("notOneortwo", function(value, element) {
	var aint = parseInt(value);
	 return (aint == 1||aint == 2)&& (aint + "") == value; 
}, "只能为1或2");
//正数小数
jQuery.validator.addMethod("isIntOrDecimal", function(value, element) {
	 var chrnum = /^[0-9]+([.]{1}[0-9]+){0,1}$/;  
	    return this.optional(element) || (chrnum.test(value)); 
	
}, "请输入合理的数字");
//正整数
jQuery.validator.addMethod("isIntGtZero", function(value, element) {
	var aint = parseInt(value);
	return aint > 0 && (aint + "") == value;
}, "格式错误");
//手机号码  
jQuery.validator.addMethod("isTel", function(value, element) {
	var length = value.length;
	var mobile = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
	var tel = /^(\d{3,4}-?)?\d{7,9}$/g;
	return this.optional(element) || tel.test(value) || (length == 11 && mobile.test(value));
}, "请填写正确的手机号码");
//汉字 
jQuery.validator.addMethod("isChinese", function(value, element) {
	return this.optional(element) || /^[\u4e00-\u9fa5]+$/.test(value);
}, "格式错误");

//身份证
jQuery.validator.addMethod("isIDcard", function(value, element) {
	return this.optional(element) || /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/.test(value);
}, "格式错误");




//分页

goPage = function(pno, psize, tableid) {
	var itable = $("#"+tableid);
	console.log($("#"+tableid));
	var tablerows = $("#"+tableid).find("tbody").find("tr");
	var num = tablerows.length; //表格所有行数(所有记录数)
	console.log(num);
	var tableId = tableid;
	var totalPage = 0; //总页数
	var pageSize = psize; //每页显示行数
	//总共分几页 
	if(num / pageSize > parseInt(num / pageSize)) {
		totalPage = parseInt(num / pageSize) + 1;
	} else {
		totalPage = parseInt(num / pageSize);
	}
	console.log(pageSize);
	var currentPage = pno; //当前页数
	var startRow = (currentPage - 1) * pageSize + 1; //开始显示的行  31 
	var endRow = currentPage * pageSize; //结束显示的行   40
	endRow = (endRow > num) ? num : endRow;

	console.log(endRow);
	//遍历显示数据实现分页
	for(var i = 1; i < (num + 1); i++) {
		var irow = tablerows.eq(i - 1);
		if(i >= startRow && i <= endRow) {
			irow.css("display", "table-row");
		} else {
			irow.css("display", "none");
		}
	}
	
	
	var tempStr = "共" + num + "条记录&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;分" + totalPage + "页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	if(currentPage > 1) {
		tempStr += "<a href=\"#\" onClick=\"goPage(" + (1) + "," + psize + ",'"+tableId+"')\">首页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>";
		tempStr += "<a href=\"#\" onClick=\"goPage(" + (currentPage - 1) + "," + psize + ",'"+tableId+"')\"><上一页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>"
	} else {
		tempStr += "首页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		tempStr += "<上一页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	}
	tempStr += "第" + currentPage + "页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
	
	if(currentPage < totalPage) {
		tempStr += "<a href=\"#\" onClick=\"goPage(" + (currentPage + 1) + "," + psize + ",'"+tableId+"')\">下一页>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>";
		tempStr += "<a href=\"#\" onClick=\"goPage(" + (totalPage) + "," + psize + ",'"+tableId+"')\">尾页</a>";
	} else {
		tempStr += "下一页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		tempStr += "尾页";
	}
	console.log(num);
	
	if(num > pageSize){
		$("#barcon").html(tempStr);
	}
}

//自动计算
var caculator =function(quantity,price,total,discount,finalTotal) {
	var num1 = $("input[name='"+quantity+"']").val() * $("input[name='"+price+"']").val();
	var num2 = $("input[name='"+discount+"']").val() * num1;
	console.log($("input[name='"+discount+"']").val());
	$("input[name='"+total+"']").val(num1);
	$("input[name='"+finalTotal+"']").val(num2);
}

/*$(function(){
	if(sessionStorage.getItem("existUser") == null){
		$("#userName").html("尚未登录，<a href='login.ftl'>点击登录</a>");
		$("#userRole").text("角色：游客");
		$("#person").css("visibility","hidden");
	}else{
		$("#userName").text("欢迎您，"+sessionStorage.getItem("existUser").username);
		$("#userRole").text("角色："+Test);
	}
	$("#person").click(function(){
		console.log(1);
		$.dialog({
			type: 'confirm',
			contentHtml: '<p style:"magrin:0 auto">确认注销？</p>',
			onClickOk: function() {
				$.ajax({
					url: "LoginAction_LoginOut",
					success: function() {
						sessionStorage.clear();//清空session
						location.href="index.ftl";
					}
				});
			}
		});
	})
	
	
});
*/

//异步下拉
//ajaxSelect = function(selectName,url,value,content){
//	$.post(url, function(data) {
//		$("select[name='"+selectName+"']").empty();
//		for(var i = 0; i < data.length; i++) {
//			$("select[name='"+selectName+"']").append("<option value='" + data[i].value + "'>" + data[i].content + "</option>");
//			}
//	});
//}
var dialogOK = function(){
	$.dialog({
		type: 'info',
		contentHtml: '<img class="info-icon" src="dialog/images/icon/success.png" alt="更新成功" /><p class="info-text">更新成功</p>',
		autoClose: 2000
	});
}


